<script setup name="Logininfor" lang="ts">
import { parseTime } from '../../../utils/ruoyi.ts'
import { list, unlockLoginInfo } from '@/api/monitor/loginInfo'
import type { LoginInfoQuery, LoginInfoVO } from '@/api/monitor/loginInfo/types'

const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { sys_device_type } = toRefs<any>(proxy?.useDict('sys_device_type'))
const { sys_common_status } = toRefs<any>(proxy?.useDict('sys_common_status'))

const loginInfoList = ref<LoginInfoVO[]>([])
const loading = ref(true)
const modal = useModal()
const message = useMessage()
const total = ref(0)
const defaultSort = ref<any>({ prop: 'loginTime', order: 'descending' })

const queryFormRef = ref()

// 查询参数
const queryParams = ref<LoginInfoQuery>({
  pageNum: 1,
  pageSize: 10,
  ipaddr: '',
  userName: '',
  status: '',
  orderByColumn: defaultSort.value.prop,
  isAsc: defaultSort.value.order,
})

/** 查询登录日志列表 */
async function getList() {
  loading.value = true
  const res = await list(queryParams.value)
  loginInfoList.value = res.rows
  total.value = res.total
  loading.value = false
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields()
  queryParams.value.pageNum = 1
}
/** 解锁按钮操作 */
async function handleUnlock(username: string) {
  modal.confirm({
    title: `是否确认解锁用户"${username}"数据项?`,
    onOk: async () => {
      await unlockLoginInfo(username)
      message.success(`用户${username}解锁成功`)
    },
  })
}
onMounted(() => {
  getList()
})
</script>

<template>
  <div class="p-2">
    <a-card class="mb-20px">
      <a-form ref="queryFormRef" :model="queryParams" class="inline-form" layout="inline" label-width="68px">
        <a-form-item label="登录地址" prop="ipaddr">
          <a-input v-model:value="queryParams.ipaddr" placeholder="请输入登录地址" clearable style="width: 240px;" @keyup.enter="handleQuery" />
        </a-form-item>
        <a-form-item label="用户名称" prop="userName">
          <a-input v-model:value="queryParams.userName" placeholder="请输入用户名称" clearable style="width: 240px;" @keyup.enter="handleQuery" />
        </a-form-item>
        <a-form-item label="状态" prop="status">
          <a-select v-model:value="queryParams.status" placeholder="登录状态" clearable style="width: 240px">
            <a-select-option v-for="dict in sys_common_status" :key="dict.value" :label="dict.label" :value="dict.value">
              {{ dict.label }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item>
          <a-space>
            <a-button type="primary" @click="handleQuery">
              搜索
            </a-button>
            <a-button @click="resetQuery">
              重置
            </a-button>
          </a-space>
        </a-form-item>
      </a-form>
    </a-card>
    <div>
      <a-card shadow="hover">
        <a-table
          v-loading="loading"
          :pagination="false"
          :data-source="loginInfoList"
        >
          <a-table-column
            title="用户名称"
            align="center"
            data-index="userName"
            :show-overflow-tooltip="true"
            sortable="custom"
            :sort-orders="['descending', 'ascending']"
          />
          <a-table-column title="客户端" align="center" data-index="clientKey" :show-overflow-tooltip="true" />
          <a-table-column title="设备类型" align="center">
            <template #default="{ record }">
              <bh-dict-tag :options="sys_device_type" :value="record.deviceType" />
            </template>
          </a-table-column>
          <a-table-column title="地址" align="center" data-index="ipaddr" :show-overflow-tooltip="true" />
          <a-table-column title="登录地点" align="center" data-index="loginLocation" :show-overflow-tooltip="true" />
          <a-table-column title="操作系统" align="center" data-index="os" :show-overflow-tooltip="true" />
          <a-table-column title="浏览器" align="center" data-index="browser" :show-overflow-tooltip="true" />
          <a-table-column title="登录状态" align="center" data-index="status">
            <template #default="{ record }">
              <bh-dict-tag :options="sys_common_status" :value="record.status" />
            </template>
          </a-table-column>
          <a-table-column title="描述" align="center" data-index="msg" :show-overflow-tooltip="true" />
          <a-table-column title="访问时间" align="center" data-index="loginTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
            <template #default="{ record }">
              <span>{{ parseTime(record.loginTime) }}</span>
            </template>
          </a-table-column>
          <a-table-column title="操作" fixed="right" align="center" class-name="small-padding fixed-width">
            <template #default="{ record }">
              <a-button v-hasPermi="['monitor:logininfor:unlock']" type="link" @click="handleUnlock(record.userName)">
                解锁
              </a-button>
            </template>
          </a-table-column>
        </a-table>
        <div class="text-right mt-5">
          <a-pagination
            v-show="total > 0"
            v-model:current="queryParams.pageNum" v-model:pageSize="queryParams.pageSize" :total="total"
            show-less-items @change="getList"
          />
        </div>
      </a-card>
    </div>
  </div>
</template>
